<?php

class Admin_Form_User extends Twitter_Bootstrap_Form_Horizontal
{

    public function getFormId()
    {
        $className = get_class($this);
        return strtolower($className);
    }

    public function init()
    {
        $this->setIsArray(true);
        //$this->setElementsBelongTo('bootstrap');

        $this->_addClassNames('form-horizontal');

        // Username
        $this->addElement('text', 'username', array(
            'id' => $this->getFormId() . '_username',
            'label' => $this->getView()->translate('USERNAME'),
            'maxlength' => 45,
            'class' => 'input-xlarge'
        ));
        $this->getElement('username')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addFilter('StringToLower')
        ->addValidators(array(
            array('NotEmpty', true),
            array('stringLength', false, array(3, 45)),
        ));

        // First name
        $this->addElement('text', 'firstname', array(
            'id' => $this->getFormId() . '_firstname',
            'label' => $this->getView()->translate('FIRSTNAME'),
            'maxlength' => 45,
            'class' => 'input-xlarge'
        ));
        $this->getElement('firstname')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true),
            array('stringLength', false, array(3, 45)),
        ));

        // Last name
        $this->addElement('text', 'lastname', array(
            'id' => $this->getFormId() . '_lastname',
            'label' => $this->getView()->translate('LASTNAME'),
            'maxlength' => 45,
            'class' => 'input-xlarge'
        ));
        $this->getElement('lastname')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true),
            array('stringLength', false, array(3, 45)),
        ));

        // Email
        $this->addElement('text', 'email', array(
            'id' => $this->getFormId() . '_email',
            'label' => $this->getView()->translate('EMAIL'),
            'maxlength' => 100,
            'class' => 'input-xlarge'
        ));
        $this->getElement('email')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true),
            array('stringLength', false, array(3, 100)),
            array('EmailAddress')
        ));

        // Published
        $this->addElement('radio', 'published', array(
            'id' => $this->getFormId() . '_published',
            'label' => $this->getView()->translate('PUBLISHED'),
            'multiOptions' => array('yes' => $this->getView()->translate('YES_'), 'no' => $this->getView()->translate('NO_')),
            'value' => 'yes'
        ));
        $this->getElement('published')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true)
        ));

        // Gender
        $this->addElement('select', 'gender', array(
            'id' => $this->getFormId() . '_gender',
            'label' => $this->getView()->translate('GENDER'),
            'multiOptions' => array('1' => $this->getView()->translate('MALE'), '0' => $this->getView()->translate('FEMALE'))
        ));
        $this->getElement('gender')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true)
        ));

        // Images
        $config = Zend_Registry::get('configuration');
        $this->addElement('file', 'file', array(
            'id' => $this->getFormId() . '_file',
            'label' => $this->getView()->translate('PHOTO')
        ));
        $this->getElement('file')
        ->setDestination($config->upload->path . DS . 'images' . DS . 'users')
        ->addValidators(array(
            array('Count', false, 1),
            array('Size', false, $config->upload->image->maxsize),
            array('Extension', false, explode(',', $config->upload->image->extension)),
            array('IsImage', false, array('magicfile' => false))
        ));

        // Images preview
        $this->addElement('render', 'image', array(
            'id' => $this->getFormId() . '_image',
            'viewPath' => 'form/element-user-image-preview.phtml',
            'width' => '120px',
            'height' => '160px'
        ));

        // DOB preview
        $this->addElement('render', 'dob', array(
            'id' => $this->getFormId() . '_dob',
            'label' => $this->getView()->translate('DOB'),
            'readonly' => 'readonly',
            'format' => 'dd-mm-yyyy',
            'viewPath' => 'form/element-datetime-picker.phtml'
        ));
        $this->getElement('dob')
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->setRequired(true)
        ->addValidators(array(
            array('NotEmpty', true)
        ));

        // Id
        $this->addElement('hidden', 'id', array(
            'id' => $this->getFormId() . '_id'
        ));
        $this->getElement('id')->removeDecorator('Wrapper')->removeDecorator('HtmlTag');
    }

}